//
//  WH_HeaderView.m
//  App_6.23
//
//  Created by wanghao on 16/6/24.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_HeaderView.h"

@implementation WH_HeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        
    }
    return self;
}
-(void)createButton{
    
    NSArray *btnArr = [NSArray arrayWithObjects:@"人气", @"最新", @"进度", @"总需人次", nil];
    
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitle:btnArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tag = 1000 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(@0);
            make.left.mas_equalTo(@(SCREEN_WIDTH / 4 * i-10));
            make.width.mas_equalTo(@(SCREEN_WIDTH / 4));
            make.height.mas_equalTo(@50);
        }];
    }
    
    UILabel *xiLabel = [UILabel new];
    xiLabel.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
    [self addSubview:xiLabel];
    [xiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self);
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
        
    }];
    self.selectView = [[UIView alloc] init];
    self.selectView.backgroundColor = [UIColor colorWithRed:0.82 green:0.13 blue:0.3 alpha:1];
    [self addSubview:self.selectView];
    
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(@0);
        make.left.mas_equalTo(@(SCREEN_WIDTH / 16-10));
        make.width.mas_equalTo(@(SCREEN_WIDTH / 8));
        make.height.mas_equalTo(@2);
    }];
    
    UIButton * button = (UIButton *)[self viewWithTag:1000];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
}

-(void)buttonAction:(UIButton *)button{
    
    [self.delegate changeButton:button.tag];

    if (button.tag != 1000) {
        
        UIButton * buttonA = (UIButton *)[self viewWithTag:1000];
        [buttonA setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    
    if (![button isEqual:self.beforeButton]) {
        
        [self.beforeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        CGFloat x = (button.tag - 1000) * (SCREEN_WIDTH / 4) + (SCREEN_WIDTH / 16-10);
        
        [self.selectView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(@(x));
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            [self layoutIfNeeded];
            
        }];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        self.beforeButton = button;
        
    }
    
}

@end
